<%--
  Created by IntelliJ IDEA.
  User: Ly
  Date: 2017/9/19
  Time: 19:10
  Content:  编辑页面
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fnx" uri="http://java.sun.com/jsp/jstl/functionsx" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="f" uri="http://www.sctv.com/tags/form" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="/WEB-INF/views/head.jsp"/>
    <script type="text/javascript">
        $(function () {
            $("#validForm").validate({
                submitHandler: function (form) {
                    $("#validForm input[name|='dy']").each(function () {
                        var name = $(this).attr("name");
                        $(this).attr("name", name.substring(3, name.lastIndexOf("-")));
                    });
                    form.submit();
                }
            });
            $("input[name='name']").focus();
        });

        function confirmDelete() {
            return confirm("<s:message code='confirmDelete'/>");
        }

        function updateCron(jobId, cron) {
            var data = {};
            data.jobId = jobId;
            data.cron = cron;
            var obj = ajaxView("/cmscp/ext/task/updateCron.do", "json", data);

            if (obj != null) {
                var flag = obj.flag;
                if (flag) {//style="color: #9d1e15"
                    $('#updateSetView').attr("style", "color: #9d1e15");
                    $("#updateSetView").text("更新成功!");
                } else {
                    var msg = obj.msg;
                    $('#updateSetView').attr("style", "color: #00a65a");
                    $("#updateSetView").text(msg);
                }
            } else {
                $('#updateSetView').attr("style", "color: #00a65a");
                $("#updateSetView").text("更是调用异常![可能是网问题等原因.....]");
            }
            setTimeout("setTimeClear()", 1000);
        }

        function setTimeClear() {
            $("#updateSetView").text("");
        }

        function jobNameView() {
            var jobName = $('#jobName').val();

            if (jobName == "" || jobName == null) {

                $('#saveBoottn').attr("disabled", "disabled");
                $('#listBoottn').attr("disabled", "disabled");
                $('#createBoottn').attr("disabled", "disabled");

                return false;
            }
            var data = {};
            data.jobName = jobName;
            var obj = ajaxView("/cmscp/ext/task/jobNameView.do", "json", data)
            if (obj.false) {

                $('#saveBoottn').attr("disabled", "disabled");
                $('#listBoottn').attr("disabled", "disabled");
                $('#createBoottn').attr("disabled", "disabled");

            }
            $('#jobNameSet').val(jobName);

            $('#jobNameView').attr("style", "color: #9d1e15");
            $('#jobNameView').text("可以使用..");

            $('#saveBoottn').removeAttr("disabled");
            $('#listBoottn').removeAttr("disabled");
            $('#createBoottn').removeAttr("disabled");

            setTimeout("setTimeClearJobName()", 1000);
        }

        function setTimeClearJobName() {
            $("#jobNameView").text("");
        }

        function jobGroupView() {
            var jobGroup = $('#jobGroup').val();
            if (jobGroup == "" || jobGroup == null) {

                $('#saveBoottn').attr("disabled", "disabled");
                $('#listBoottn').attr("disabled", "disabled");
                $('#createBoottn').attr("disabled", "disabled");

                return false;
            }
            $('#jobGroupSet').val(jobGroup);

            $('#saveBoottn').removeAttr("disabled");
            $('#listBoottn').removeAttr("disabled");
            $('#createBoottn').removeAttr("disabled");

        }

        function ajaxView(mothe, datatype, data) {
            var obj;
            var strurl = window.location.host;
            $.ajax({
                type: "POST",
                url: "http://" + strurl + mothe,
                dataType: datatype,
                data: data,
                async: false,
                success: function (msg) {
                    obj = msg;
                },
                error: function (msg) {
                }
            });
            return obj;
        }
    </script>
</head>
<body class="skin-blue content-body">
<jsp:include page="/WEB-INF/views/commons/show_message.jsp"/>
<div class="content-header">
    <h1>定时任务 - <s:message code="${oprt=='edit' ? 'edit' : 'create'}"/></h1>
</div>
<c:set var="numberExist"><s:message code="vote.number.exist"/></c:set>
<div class="content">
    <div class="box box-primary">
        <form class="form-horizontal" id="validForm" action="${oprt=='edit' ? 'saveOrUpdate' : 'saveOrUpdate'}.do"
              method="post">
            <tags:search_params/>
            <f:hidden name="id" value="${bean.id}"/>
            <f:hidden name="position" value="${position}"/>
            <input type="hidden" name="createTime"
                   value="<fmt:formatDate value="${bean.createTime}" pattern="yyyy-MM-dd'T'HH:mm:ss"/>"
                   onclick="WdatePicker({dateFmt:'yyyy-MM-ddTHH:mm:ss'});" class="form-control"
            />
            <input type="hidden" id="redirectLive" name="redirect" value="edit"/>
            <div class="box-header with-border">
                <div class="btn-toolbar">
                    <div class="btn-group">
                        <shiro:hasPermission name="ext:vote:create">
                            <button class="btn btn-default" type="button"
                                    onclick="location.href='create.do';"<c:if
                                    test="${oprt=='create'}"> disabled="disabled"</c:if>><s:message
                                    code="create"/></button>
                        </shiro:hasPermission>
                    </div>
                    <div class="btn-group">
                        <shiro:hasPermission name="ext:vote:delete">
                            <button class="btn btn-default" type="button"
                                    onclick="if(confirmDelete()){location.href='delete.do?ids=${bean.id}';}"
                                    <c:if test="${oprt=='create'}"> disabled="disabled"</c:if>><s:message
                                    code="delete"/></button>
                        </shiro:hasPermission>
                    </div>

                    <div class="btn-group">
                        <button class="btn btn-default" type="button"
                                onclick="location.href='list.do';"><s:message
                                code="return"/></button>
                    </div>
                </div>
            </div>
            <div class="box-body">
                <div class="row">
                    <div class="col-sm-8">
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><em class="required">*</em>名称</label>
                            <div class="col-sm-10">
                                <input type="text" id="jobName" value="${bean.jobName}"
                                       class="form-control required" maxlength="150"
                                        <c:if test="${bean.id==null}">
                                            onblur="return jobNameView()"
                                        </c:if>
                                        <c:if test="${bean.id!=null}">
                                            disabled="disabled"
                                        </c:if>

                                />
                                <input type="hidden" value="${bean.jobName}" name="jobName" id="jobNameSet">
                                <span id="jobNameView"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><em class="required">*</em>分组</label>
                            <div class="col-sm-10">
                                <input type="text" id="jobGroup" value="${bean.jobGroup}"
                                       class="form-control required" maxlength="150"
                                        <c:if test="${bean.id==null}">
                                            onblur="return jobGroupView()"
                                        </c:if>
                                        <c:if test="${bean.id!=null}">
                                            disabled="disabled"
                                        </c:if>
                                />
                                <input type="hidden" id="jobGroupSet" name="jobGroup" value="${bean.jobGroup}"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-8">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">实体类[包名+类名]: </label>
                            <div class="col-sm-10">
                                <f:text name="beanClass" value="${bean.beanClass}"
                                        class="form-control required" rows="5"/>
                                <span style="color: #9d1e15">普通</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><em class="required">*</em>别名[Bean]: </label>
                            <div class="col-sm-10">
                                <f:text name="springId" value="${bean.springId}"
                                        class="form-control required" rows="5"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><em class="required">*</em>任务调用的方法名: </label>
                            <div class="col-sm-10">
                                <f:text name="methodName" value="${bean.methodName}"
                                        class="form-control required" rows="5"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-8">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态： </label>
                            <div class="col-sm-8">
                                <div class="col-sm-4">
                                    <select class="form-control" name="jobStatus">
                                        <f:option value="0" selected="${bean.jobStatus}">关闭</f:option>
                                        <f:option value="1" selected="${bean.jobStatus}">启动</f:option>
                                    </select>
                                </div>
                                <label class="col-sm-4 control-label">任务状态:</label>
                                <div class="col-sm-4">
                                    <select class="form-control" name="isRestore">
                                        <f:option value="0" selected="${bean.isRestore}">正常</f:option>
                                        <f:option value="1" selected="${bean.isRestore}">恢复</f:option>
                                        <f:option value="2" selected="${bean.isRestore}">暂停</f:option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-8">
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><em class="required"></em>定时任务表达式：</label>
                            <div class="col-sm-6">
                                <select class="form-control" name="cronExpression" id="cronExpression">
                                    <f:option value="*/5 * * * * ?"
                                              selected="${bean.cronExpression}"> 每隔5秒执行一次</f:option>
                                    <f:option value="0 */1 * * * ?"
                                              selected="${bean.cronExpression}"> 每隔1分钟执行一次</f:option>
                                    <f:option value="0 */5 * * * ?"
                                              selected="${bean.cronExpression}"> 每隔5分钟执行一次</f:option>
                                    <f:option value="0 */10 * * * ?"
                                              selected="${bean.cronExpression}"> 每隔10分钟执行一次</f:option>
                                    <f:option value="0 */30 * * * ?"
                                              selected="${bean.cronExpression}"> 每隔30分钟执行一次</f:option>
                                    <f:option value="0 0 */1 * * ?"
                                              selected="${bean.cronExpression}"> 每隔1个小时执行一次</f:option>
                                    <f:option value="0 0 */3 * * ?"
                                              selected="${bean.cronExpression}"> 每隔3个小时执行一次</f:option>
                                    <f:option value="0 0 */12 * * ?"
                                              selected="${bean.cronExpression}"> 每隔12个小时执行一次</f:option>
                                    <f:option value="0 0 23 * * ?"
                                              selected="${bean.cronExpression}"> 每天23点执行一次</f:option>
                                    <f:option value="0 0 1 * * ?"
                                              selected="${bean.cronExpression}">每天凌晨1点执行一次</f:option>
                                    <f:option value="0 15 10 ? * MON-FRI"
                                              selected="${bean.cronExpression}">每个周一,二, 三和周四的 10:15 AM</f:option>
                                    <f:option value="0 0 1 ? * L"
                                              selected="${bean.cronExpression}">每周星期天凌晨1点实行一次</f:option>
                                    <f:option value="0 15 10 15 * ?"
                                              selected="${bean.cronExpression}">每月15号的 10:15 AM</f:option>
                                    <f:option value="0 15 10 ? * 6L"
                                              selected="${bean.cronExpression}">每月最后一个周五的 10:15 AM</f:option>
                                </select>
                            </div>
                            <shiro:hasPermission name="ext:task:updateCron">
                            <button class="btn btn-default" type="button"
                                    onclick="return  updateCron(${bean.id},$('#cronExpression').val())"
                                    <c:if test="${oprt=='create'}">
                                        disabled="disabled"
                                    </c:if>
                                    <c:if test="${bean.jobStatus == '0'}">
                                        disabled="disabled"
                                    </c:if>>
                                更新定时任务时间
                            </button>
                            </shiro:hasPermission>
                            <span id="updateSetView"></span>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-sm-8">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">执行方式： </label>
                            <div class="col-sm-8">
                                <select class="form-control" name="isConcurrent" id="isConcurrent">
                                    <f:option value="1"
                                              selected="${bean.isConcurrent}"> 计划执行处[无状态]</f:option>
                                    <f:option value="0"
                                              selected="${bean.isConcurrent}"> 执行[方法执行没有结束,新的【 轮询阻塞 】]</f:option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-sm-8">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">参数设置： </label>
                            <div class="col-sm-8">
                                <div class="col-sm-8">
                                    <f:text class="form-control" name="parSet" value="${bean.parSet}"/>
                                    <span style="color: #9d1e15">*[ json格式的数据、数字、不填写 ]</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <%-----------------------------------------------------------[  按钮 保存 如下:  ]----------------------------------------------------------------------------------%>
                <div class="box-footer">
                        <button class="btn btn-primary" type="submit" id="saveBoottn"
                                onclick="$('#redirectLive').val('edit');">
                            <s:message
                                    code="save"/></button>
                        <button class="btn btn-default" type="submit" id="listBoottn"
                                onclick="$('#redirectLive').val('list');">
                            <s:message
                                    code="saveAndReturn"/></button>

                        <c:if test="${oprt=='create'}">
                            <button class="btn btn-default" type="submit" id="createBoottn"
                                    onclick="$('#redirectLive').val('create');">
                                <s:message
                                        code="saveAndCreate"/></button>
                        </c:if>
                </div>
            </div>
        </form>
    </div>
</div>
</div>
</body>
</html>
